package com.transcargo.request;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.sql.SQLException;

import com.transcargo.action.ServletParametersException;
import com.transcargo.action.ServletValidationException;
import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.geo.Country;
import com.transcargo.geo.Region;
import com.transcargo.geo.City;
import com.transcargo.geo.GeoObjectNotFoundException;
import com.transcargo.rpc.RPCOutput;


public class MatchCityRequest extends Request
{
	public static class MatchCity {
		int CountryID;
		int RegionID;
		int CityID;
	}
	private String name = "";
	private String val = "";
	private String xml = "";

	private void servletGetValues(HttpServletRequest request) throws ServletParametersException {
		name = request.getParameter("name");
		if (name == null) name = "";
		val = request.getParameter("val");
		if (val == null) val = "";
    }
    private void servletValidateValues() throws ServletValidationException {
    	if (name.length() == 0) throw new ServletValidationException("Empty name");
    }
    private void servletExecuteRequest() throws ServletRequestException {
        xml =  RPCOutput.generate(false, "val", val);
    	try {
			db.PreStatement pre = dbase.CreateStatement("SELECT GetCityMatch(?)");
			pre.Insert(name);
            list = dbase.execute(pre);
			if (list != null) {
				iter = list.iterator();
				if (iter.hasNext()) {
					String[] res = iter.next();
					if (res[0].length() > 0) {
						StringTokenizer tok = new StringTokenizer(res[0],"-");
						int n = tok.countTokens();
						int[] w = new int[n];
						for (int i = 0; i < n; i++) w[i] = Integer.parseInt(tok.nextToken());
						MatchCity c = new MatchCity();
						c.CountryID = w[0];
						c.RegionID = w[1];
						c.CityID = w[2];
						xml =  RPCOutput.generate(c, "val", val);
					}
				}
			}
    	} catch (dbException e)
		{
			System.out.println("dbException: " + e.getMessage());
			throw new ServletRequestException();
		} catch (SQLException e)
		{
			System.out.println("SQLException: " + e.getMessage());
			throw new ServletRequestException();
		}
    }
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        PrintWriter out = null;
		try {
            out = response.getWriter();
            servletGetValues(request);
            servletValidateValues();
            servletExecuteRequest();
            out.println(xml);
	    } catch (ServletParametersException e) {
           sendRedirect(response, "index.jsp");
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletRequestException e) {
            String xml =  RPCOutput.generate(false);
            out.println(xml);
        }  catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        }
    }    

}
